package littlepicture.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import littlepicture.data.dao.EmployeeDao;
import littlepicture.data.dao.RecordDao;
import littlepicture.data.model.Employee;
import littlepicture.data.model.WorkRecord;
import littlepicture.data.template.EmployeeDailyWork;
import littlepicture.data.template.EmployeeDate;
import littlepicture.data.template.ModulePersons;
import littlepicture.data.tools.DaoService;
import littlepicture.data.tools.DateUtils;
import littlepicture.pojo.DailyRecords;
import littlepicture.pojo.LpProject;
import littlepicture.pojo.Module;
import littlepicture.pojo.Person;
import littlepicture.pojo.Record;

/**
 * @author byang
 */
public class MainServiceImp implements MainService {
	private static int DAYS_OF_WEEK = 5;

	public LpProject getLpProject(Date currentDate) {
		LpProject project = new LpProject();
		
		List<Module> modules = new ArrayList<Module>();
		List<ModulePersons> modulePersons = getEmployeeDao().getModulePersons();
		for (Iterator<ModulePersons> iterator = modulePersons.iterator(); iterator.hasNext();) {
			ModulePersons modulePerson = (ModulePersons) iterator.next();
			Module module = new Module();
			module.setName(modulePerson.getModule());
			List<Person> persons = new ArrayList<Person>();
		    for (Employee employee : modulePerson.getPersons()) {
		    	Person person = new Person();
		    	person.setName(employee.getName());
		    	person.setEmployeeId(employee.getEmployeeId());
		    	populatePersonWeeklyRecords(person, currentDate);
		    	persons.add(person);
			}
			module.setPersons(persons);
			modules.add(module);
		}
		project.setModules(modules);
		return project;
	}
	
	private void populatePersonWeeklyRecords(Person person, Date startTime){
		DailyRecords[] weeklyRecords = new DailyRecords[5];
		EmployeeDate eeDate = new EmployeeDate();
		eeDate.setEmployeeId(person.getEmployeeId());
		
		int days = DAYS_OF_WEEK;
		while(days > 0){
			eeDate.setDate(startTime);
			List<EmployeeDailyWork> dailyWorks = getRecordDao().getEmployeeDailyWork(eeDate);
			DailyRecords dailyRecords = new DailyRecords();
			dailyRecords.setDate(startTime);
			weeklyRecords[DAYS_OF_WEEK - days] = dailyRecords;
			if(dailyWorks.size() > 0){
				List<Record> records = dailyWorks.get(0).getRecords();
				dailyRecords.setRecords(records);
			}
			startTime = DateUtils.getNextDay(startTime);
			days--;
		}
		person.setWeeklyRecords(weeklyRecords);
	}
	
	public long addWorkRecordForPerson(int employeeId, Date date, Record record){
		if(record != null ){
			WorkRecord workRecord = new WorkRecord();
			workRecord.setRecordDate(date);
			workRecord.setContent(record.getContent());
			workRecord.setRecordStatus(record.getStatus());
			workRecord.setEmployeeId(employeeId);
			return getRecordDao().addRecord(workRecord);
		}
		return 0;
	}
	
	public void deleteWorkRecordById(long workRecordId){
		getRecordDao().deleteRecord(workRecordId);
	}
	
	public void updateWorkRecord(Record record){
		WorkRecord workRecord = getRecordDao().getWorkRecordById(record.getRecordId());
		if(workRecord != null){
			workRecord.setContent(record.getContent());
			workRecord.setRecordStatus(record.getStatus());
			getRecordDao().updateRecord(workRecord);
		}
	}
	
	private EmployeeDao getEmployeeDao(){
		return (EmployeeDao)DaoService.getDao(EmployeeDao.class);
	}
	
	private RecordDao getRecordDao(){
		return (RecordDao)DaoService.getDao(RecordDao.class);
	}

	public static void main(String[] args){
		MainServiceImp msi = new MainServiceImp();
		msi.getLpProject(new Date());
	}
}
